package com.neoCalf.neoCommon.utils;

import java.util.Map;
import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author 小牛
 * @version 1.0
 * @description TODO
 * @date 2025/4/13 1:44
 */
public class SessionSeq {
    private final Map<String, AtomicLong> sessionSeqs = new ConcurrentHashMap<>();

    public long next(String sessionId) {
        long timestamp = System.currentTimeMillis(); // 获取当前时间戳
        AtomicLong counter = sessionSeqs.computeIfAbsent(sessionId, k -> new AtomicLong(0));
        long seq = counter.incrementAndGet(); // 增加局部计数器

        // 将时间戳和局部计数器结合生成一个唯一的long序列号
        return (timestamp << 20) | (seq & 0xFFFFF); // 左移并将计数器的低位拼接到时间戳后
    }
}